package top.soulblack.purchasesale.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.mapper.BarCodeMapper;
import top.soulblack.purchasesale.model.*;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BarCodeService {
    @Autowired
    private BarCode barcode;

    @Resource
    private BarCodeMapper barcodeMapper;

    public BarCode selectbarcode() { return this.barcode; }

    public void setproduct(boolean _static, String factoryCode, String startCode,int barcodeItem){
        this.barcode.setProduct_static(_static);
        this.barcode.setProduct_barcode_forward(factoryCode);
        this.barcode.setProduct_barcode_later(startCode);
        this.barcode.setlast_static(barcodeItem);
    }

    public void setsingle(boolean _static, String factoryCode, String startCode,int barcodeItem){
        this.barcode.setProduct_static(_static);
        this.barcode.setSingle_barCode_forward(factoryCode);
        this.barcode.setSingle_barCode_later(startCode);
        this.barcode.setlast_static(barcodeItem);
    }

    public BarCode select_max(){
        BarCode now = new BarCode();
        // single
        now.setSingle_barCode_forward(this.barcode.getSingle_barCode_forward());
        String a=barcodeMapper.select_max_only_bar_code(this.barcode.getSingle_barCode_forward());
        if(a!=null&&a.length()!=0)
            a=a.substring(8,12);
        int start;
        String getSingle_barCode_later=this.barcode.getSingle_barCode_later().substring(0, 4);
        if(a==null||getSingle_barCode_later.compareTo(a)>0) {
            start = Integer.parseInt(getSingle_barCode_later);
        }
        else{
            start=Integer.parseInt(a);
            start += 1;
        }
        now.setSingle_barCode_later(String.valueOf(start));
        // project
        now.setProduct_barcode_forward(this.barcode.getSingle_barCode_forward());
        a=barcodeMapper.select_max_bar_code(this.barcode.getProduct_barcode_forward());
        if(a!=null&&a.length()!=0)
            a=a.substring(8,12);
        String getProduct_barcode_later=this.barcode.getProduct_barcode_later().substring(0, 4);
        if(a==null||getProduct_barcode_later.compareTo(a)>0) {
            start = Integer.parseInt(getProduct_barcode_later);
        }
        else{
            start=Integer.parseInt(a);
            start += 1;
        }
        now.setProduct_barcode_later(String.valueOf(start));
        return now;
    }

    public String generate(int barcodeItem){
        if(barcodeItem==1){
            String a=barcodeMapper.select_max_bar_code(this.barcode.getProduct_barcode_forward());
            if(a!=null&&a.length()!=0)
                a=a.substring(8,12);
            int start;
            String getProduct_barcode_later=this.barcode.getProduct_barcode_later().substring(0, 4);
            if(a==null||getProduct_barcode_later.compareTo(a)>0) {
                start = Integer.parseInt(getProduct_barcode_later);
            }
            else{
                start=Integer.parseInt(a);
                start += 1;
            }
            List<Goods> bar_code=barcodeMapper.select_bar_code();
            StringBuilder sb = new StringBuilder();
            sb=sb.append(this.barcode.getProduct_barcode_forward());
            int len=bar_code.size();
            if(len <= 10000-start && start!=10000){
                for(int i =0;i<len;i++){
                    sb.setLength(8);
                    sb=sb.append(String.format("%04d", i+start));
                    int a0 = 0, b = 0, c = 0, d = 0;
                    for (int i0 = 0; i0 < 12; i0 += 2) {
                        a0 += (sb.charAt(i0) - '0');
                        b += (sb.charAt(i0 + 1) - '0');
                    }
                    c = (a0 + b * 3) % 10;
                    d = (10 - c) % 10;
                    sb=sb.append(d);
                    barcodeMapper.updatebarcode(sb.toString(),bar_code.get(i).getId());
                }
                return String.valueOf(len);
            }
            else
                return "-1";
        }
        else{
            String a=barcodeMapper.select_max_only_bar_code(this.barcode.getSingle_barCode_forward());
            if(a!=null&&a.length()!=0)
              a=a.substring(8,12);
            int start;
            String getSingle_barCode_later=this.barcode.getSingle_barCode_later().substring(0, 4);
            if(a==null||getSingle_barCode_later.compareTo(a)>0) {
                start = Integer.parseInt(getSingle_barCode_later);
            }
            else{
                start=Integer.parseInt(a);
                start += 1;
            }
            List<Goods> bar_code=barcodeMapper.select_only_bar_code();
            StringBuilder sb = new StringBuilder();
            sb=sb.append(this.barcode.getSingle_barCode_forward());
            int len=bar_code.size();
            if(len <= 10000-start && start!=10000 ){
                for(int i =0;i<len;i++){
                    sb.setLength(8);
                    sb=sb.append(String.format("%04d", i+start));
                    int a0 = 0, b = 0, c = 0, d = 0;
                    for (int i0 = 0; i0 < 12; i0 += 2) {
                        a0 += (sb.charAt(i0) - '0');
                        b += (sb.charAt(i0 + 1) - '0');
                    }
                    c = (a0 + b * 3) % 10;
                    d = (10 - c) % 10;
                    sb=sb.append(d);
                    barcodeMapper.updateonlybarcode(sb.toString(),bar_code.get(i).getId());
                }
                return String.valueOf(len);
            }
            else
                return "-2";
        }
    }

    public boolean check_barcode(String code){
        if (code == null || code.length() != 13)
            return false;
        int a = 0, b = 0, c = 0, d = 0;
        for (int i = 0; i < 12; i += 2) {
            a += (code.charAt(i) - '0');
            b += (code.charAt(i + 1) - '0');
        }
        c = (a + b * 3) % 10;
        d = (10 - c) % 10;
        return (code.charAt(12)-'0')==d;
    }
}
